package com.coocaa.x.service.liteservice;

import android.content.Context;
import android.os.RemoteException;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.service.liteservice.aidl.ILiteService;

public class LiteServiceManager {
    public static final String LITE_SERVICE_ON_CREATE_BROADCAST_ACTION = "coocaa.liteservice.action.ON_CREATE";
    public static final String LITE_SERVICE_ON_DESTROY_BROADCAST_ACTION = "coocaa.liteservice.action.ON_DESTROY";
    private static boolean bBind = false;

    public synchronized static void bind(final Context context) {
        if (bBind)
            return;
        bBind = true;
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                LiteEntryServiceConnecter.connecter.bindService(context, context.getPackageName());
            }
        });
    }

    public synchronized static void unbind(Context context) {
        if (!bBind)
            return;
        LiteEntryServiceConnecter.connecter.unbindService(context);
        bBind = false;
    }

    public static ILiteService getILiteService() {
        try {
            return LiteEntryServiceConnecter.connecter.getService().getLiteService();
        } catch (RemoteException e) {
            e.printStackTrace();
            return null;
        }
    }
}
